package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.dto.order.CourseWebDto;
import com.atguigu.eduservice.client.OrderClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.vo.ChapterVo;
import com.atguigu.eduservice.entity.vo.CourseQueryVo;
import com.atguigu.eduservice.entity.vo.CourseWebVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/course/front")
@CrossOrigin
@Slf4j
public class FrontCourseController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrderClient orderClient;
    @Autowired
    private EduVideoService eduVideoService;

    @ApiOperation(value = "分页课程列表")
    @PostMapping(value = "{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery){
        Page<EduCourse> pageParam = new Page<EduCourse>(page, limit);
        Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
        return  R.ok().data(map);
    }


    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId ,HttpServletRequest request){

        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        boolean buyCourse = false;
        if(JwtUtils.checkToken(request)){
            //远程调用，判断课程是否被购买
            buyCourse = orderClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), courseId);
        }

        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList).data("isbuy",buyCourse);
    }

    //根据课程id查询课程信息
    @GetMapping("getDto/{courseId}")
    public CourseWebDto getCourseInfoDto(@PathVariable String courseId) {
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);
        CourseWebDto courseWebDto = new CourseWebDto();
        BeanUtils.copyProperties(courseWebVo,courseWebDto);
        return courseWebDto;
    }

    /**
     *
     * 根据视频id查看课程是否免费
     * @author 粟德桡
     * @date 2021/12/4 15:12
     * @return boolean
     */
    @GetMapping("getIsFree/{videoId}")
    public boolean getIsFree(@PathVariable String videoId,HttpServletRequest request) {

        if(StringUtils.isEmpty(videoId)){

            return false;
        }
        Boolean flag = false;
        // 先看视频是否免费 传sourseID
        EduVideo one = eduVideoService.getOne(new LambdaQueryWrapper<EduVideo>().eq(EduVideo::getVideoSourceId, videoId));
        if(one!=null){
            if(one.getIsFree()==true){
                flag=true;
                return flag;
            }else {
                //非免费视频查询是否付费了
                String courseId = one.getCourseId();
                if(JwtUtils.checkToken(request)){
                    //远程调用，判断课程是否被购买
                    flag = orderClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), courseId);
                    return flag;
                }
            }

        }

        return flag;
    }


}
